/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.com.jvpn.tools.update;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 *
 * @author chenjianting
 */
public class UpdateJar {

//    public void downloadNet() throws MalformedURLException {
//        int bytesum = 0;
//        int byteread = 0;
//
//        URL url = new URL("windine.blogdriver.com/logo.gif");
//
//        try {
//            URLConnection conn = url.openConnection();
//            InputStream inStream = conn.getInputStream();
//            FileOutputStream fs = new FileOutputStream("c:/abc.gif");
//
//            byte[] buffer = new byte[1204];
//            int length;
//            while ((byteread = inStream.read(buffer)) != -1) {
//                bytesum += byteread;
////                System.out.println(bytesum);
//                fs.write(buffer, 0, byteread);
//            }
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
    public static void testkill() {
//        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
//        String name = runtime.getName(); // format: "pid@hostname"  
        try {
            File updateBat = new File("update.bat");
            if (!updateBat.exists()) {
                FileWriter pbkFile = new FileWriter("update.bat");
                InputStream inputStream = UpdateJar.class.getClassLoader().getResourceAsStream("cn/com/jvpn/resources/update");
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                try {
                    String tempString = null;
                    while ((tempString = reader.readLine()) != null) {
                        pbkFile.write(tempString + "\r\n");
                    }
                } catch (Exception e) {
                } finally {
                    reader.close();
                    pbkFile.close();
                }
            }
            Process process = Runtime.getRuntime().exec("cmd /c  start " + updateBat.getAbsolutePath());
//            int parseInt = Integer.parseInt(name.substring(0, name.indexOf('@')));
//            System.out.println(parseInt);
//            for (int i = 0; i < 100; i++) {
//                if (i == 1) {
//                    File updateBat = new File("update.bat");
//                    System.out.println(updateBat.getAbsoluteFile());
//                    Process process = Runtime.getRuntime().exec("cmd /c  start " + updateBat.getAbsolutePath());
//                    BufferedReader br = new BufferedReader(new InputStreamReader(
//                            process.getInputStream()));
//                    String line = "";
//                    System.out.println("zzz");
//
//                    while ((line = br.readLine()) != null) {
//                        System.out.println("dd");
//                        System.out.println(line);
//                    }
//                }
//
//                System.out.println(i);
//            }
        } catch (Exception e) {
            System.out.println("error");
        }
    }
}
